package com.bubblegumproject.ogo.swing.util;

import javax.swing.*;
import java.awt.*;

/**
 * Utility class that makes it easier to work with {@link javax.swing.JFrame} using a fluent interface.
 *
 * @author Azubuko Obele (buko.obele@gmail.com)
 */
public class PageFrame {

    private JFrame frame;

    public PageFrame() {
        frame = new JFrame();
    }

    public JFrame toJFrame() {
        return frame;
    }

    public PageFrame title(String title) {
        frame.setTitle(title);
        return this;
    }

    public PageFrame defaultButton(JButton button) {
        frame.getRootPane().setDefaultButton(button);
        return this;
    }

    public PageFrame add(Component component) {
        frame.add(component);
        return this;
    }

    public PageFrame menuBar(JMenuBar menuBar) {
        frame.setJMenuBar(menuBar);
        return this;
    }

    public PageFrame menuBar(MenuBarBuilder menuBarBuilder) {
        assert(menuBarBuilder != null);
        return menuBar(menuBarBuilder.result());
    }

    public PageFrame showAtScreenCenter() {
        frame.pack();
        OgoSwingUtilities.center(frame);
        frame.setVisible(true);
        return this;
    }

    public void destroy() {
        frame.dispose();
    }

    public PageFrame showErrorMessage(String title, String message) {
        assert(title != null && message != null);
        JOptionPane.showMessageDialog(frame, message, title, JOptionPane.ERROR_MESSAGE);
        return this;
    }

    public PageFrame showInformationMessage(String title, String message) {
        assert(title != null && message != null);
        JOptionPane.showMessageDialog(frame, message, title, JOptionPane.INFORMATION_MESSAGE);
        return this;
    }

    public PageFrame showWarningMessage(String title, String message) {
        assert(title != null && message != null);
        JOptionPane.showMessageDialog(frame, message, title, JOptionPane.WARNING_MESSAGE);
        return this;
    }

    public int showYesNoConfirm(String title, String message) {
        assert(title != null && message != null);
        return JOptionPane.showConfirmDialog(frame, message, title, JOptionPane.YES_NO_OPTION,
                JOptionPane.QUESTION_MESSAGE);
    }
}
